<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends Front_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library(array('pagination','security'));
        $this->load->model(array('search_model'));
    }

	public function index($offset = 0){
		$data = array();
		$keyword  =  $this->input->get_post('keyword',true);
		if($this->input->post('keyword')){
			$link = site_url('search').'?keyword='.urlencode($keyword);
			redirect($link);
		}
        $page = $this->uri->segment(3);
        if(empty($page)){
            $page=1;
        }
        $page_size = 20;
        $page_url   = base_url('search/ajax_search');
        $total_rows = $this->search_model->count_all($keyword);
        $records = $this->search_model->find_all($keyword, $page_size, $page);
        $pagination = $this->get_paging($page_url, $total_rows, $page, $page_size );
        $data = array(
            "total_rows" => $total_rows,
            "records" => $records,
            "pagination" => $pagination
        );
		$data['keyword'] = $keyword;
		$data['category_id'] = 0;
	  	$this->layout->view($data);
	}

	function ajax_search(){
        $keyword = $this->input->get_post('keyword',true);
        $page = $this->uri->segment(3);
        if(empty($page)){
            $page=1;
        }
        $page_size = 20;
        $page_url   = base_url('search/ajax_search');
        $total_rows = $this->search_model->count_all($keyword);
        $records = $this->search_model->find_all($keyword, $page_size, $page);
        $pagination = $this->get_paging($page_url, $total_rows, $page, $page_size );
        $data = array(
            "total_rows" => $total_rows,
            "records" => $records,
            "pagination" => $pagination
        );
        $this->load->view('default/search/ajax_search',$data);
	}
}

